package com.company.example.interceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by ywl on 2016/12/12.
 */
public class MyInvocationHandler implements InvocationHandler{
    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("-----------start---------");
        Object result = method.invoke(target, args);
        System.out.println("-----------end---------");
        return result;
    }

    /*
    * 获取目标对象的代理对象
     */
    public Object getProxy() {
        System.out.println(target.getClass().getClassLoader());
        System.out.println(Thread.currentThread().getContextClassLoader());
        //        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);
    }
}
